package com.xiyou.week01.stack;

import java.util.Stack;

/**
 * 20. 有效的括号
 *
 * @ClassName：LeetCode20
 * @Author：西柚
 * @Date：2021/11/20 4:58 下午
 * @Versiion：1.0
 */
public class IsValid {

    /**
     * 输入：s = "()[]{}"
     * 输出：true
     *
     * @param s
     * @return
     */
    public boolean isValid(String s) {
//        Stack<Character> stack = new Stack<Character>();
//        for (Character c : s.toCharArray()) {
//            if ('(' == c) {
//                stack.push(')');
//            } else if ('[' == c) {
//                stack.push(']');
//            } else if ('{' == c) {
//                stack.push('}');
//            } else if (stack.isEmpty() || c != stack.pop()) {
//                return false;
//            }
//        }

        Stack<String> stack = new Stack<String>();
        for (char c : s.toCharArray()) {
            if ('(' == c || '[' == c || '{' == c) {
                stack.push(String.valueOf(c));
            } else {
                if (stack.isEmpty()) return false;
                if (')' == c && !"(".equals(stack.peek())) return false;
                if (']' == c && !"[".equals(stack.peek())) return false;
                if ('}' == c && !"{".equals(stack.peek())) return false;
                stack.pop();
            }
        }

        return stack.isEmpty();
    }


    public static void main(String[] args) {
        IsValid leetCode20 = new IsValid();
        leetCode20.isValid("{}");
    }
}
